package com.youdianda.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.constant.MessageConstant;
import com.youdianda.dto.CateQueryPageDTO;
import com.youdianda.entity.YddArticle;
import com.youdianda.entity.YddCate;
import com.youdianda.mapper.YddCateMapper;
import com.youdianda.result.Result;
import com.youdianda.service.IYddArticleService;
import com.youdianda.service.IYddCateService;
import com.youdianda.vo.CateAllVO;
import com.youdianda.vo.CateQueryPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mosen
 * @since 2023-05-15
 */
@Service
public class YddCateServiceImpl extends ServiceImpl<YddCateMapper, YddCate> implements IYddCateService {
    @Autowired
    private IYddArticleService yddArticleService;

    /**
     * 查询所有分类
     *
     * @param
     * @return
     */
    @Override
    public Result<List<CateAllVO>> cateAll() {
        List<YddCate> cateAll = this.list();
        ArrayList<CateAllVO> list = new ArrayList<>();
        for (YddCate yddCate : cateAll) {
            CateAllVO cateAllVO = new CateAllVO();
            cateAllVO.setSort_num(yddCate.getSortNum());

            BeanUtils.copyProperties(yddCate, cateAllVO);
            list.add(cateAllVO);
        }

        return Result.success(list);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result<YddCate> delCate(String id) {

        this.removeById(id);
        //相关文章的分类名删除
        YddArticle cateByYddArticle = yddArticleService.getById(id);
        cateByYddArticle.setCateid(0);
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param yddCate
     * @return
     */
    @Transactional
    @Override
    public Result<YddCate> updateCate(YddCate yddCate) {
        //1.判断分类名是否唯一
        LambdaQueryWrapper<YddCate> query = new LambdaQueryWrapper<>();
        query.eq(YddCate::getCatename, yddCate.getCatename()).ne(YddCate::getId, yddCate.getId());
        long count = this.count(query);
        // 2.如果已经存在,异常处理
        if (count > 0) {
            return Result.error(MessageConstant.UPDATE_CATENAME_ALREADY_EXISTS);
        }
        //3.唯一则更新数据库
        this.updateById(yddCate);
        return Result.success();
    }

    /**
     * 根据分类id查询
     *
     * @param id
     * @return
     */
    @Override
    public Result<YddCate> CateById(String id) {

        YddCate yddCate = this.getById(id);
        return Result.success(yddCate);
    }

    /**
     * 添加分类
     *
     * @param cate
     * @return
     */
    @Transactional
    @Override
    public Result<YddCate> addCate(YddCate cate) {
        //1.判断分类名是否唯一
        LambdaQueryWrapper<YddCate> query = new LambdaQueryWrapper<>();
        query.eq(YddCate::getCatename, cate.getCatename());

        long count = this.count(query);
        // 2.如果已经存在,异常处理
        if (count > 0) {
            return Result.error(MessageConstant.ADD_CATENAME_ALREADY_EXISTS);
        }
        //3.mp添加分类
        this.save(cate);
        return Result.success();
    }

    /**
     * 分类分页查询
     *
     * @param cateQueryPageDTO
     * @return
     */
    @Override
    public Result<CateQueryPageVo> CatePage(CateQueryPageDTO cateQueryPageDTO) {
        //1.设置分页参数
        IPage<YddCate> page = new Page<>(cateQueryPageDTO.getPage(), cateQueryPageDTO.getLimit());

        //2.模糊查询分类名称
        LambdaQueryWrapper<YddCate> query = new LambdaQueryWrapper<>();
        query.like(cateQueryPageDTO.getCatename() != null, YddCate::getCatename, cateQueryPageDTO.getCatename());


        //3.根据排序大小正序
        query.orderByAsc(YddCate::getSortNum);

        //4.mp分页查询
        IPage<YddCate> tagIPage = this.page(page, query);

        //5.封装vo
        CateQueryPageVo cateQueryPageVo = new CateQueryPageVo(cateQueryPageDTO.getPage(), cateQueryPageDTO.getLimit(), tagIPage.getTotal(), tagIPage.getPages(), tagIPage.getRecords());

        return Result.success(cateQueryPageVo);
    }
}
